package main

import "fmt"

func generateMatrix(n int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		arr := make([]int, n)
		res[i] = append(res[i], arr...)
	}
	_generateMatrix(0, n-1, 0, n-1, 1, res)
	return res
}

func _generateMatrix(r1, r2, c1, c2 int, startVal int, nums [][]int) {
	if r1 > r2 || c1 > c2 {
		return
	}
	for i, j := r1, c1; j <= c2; j++ {
		nums[i][j] = startVal
		startVal++
	}
	for i, j := r1+1, c2; i <= r2; i++ {
		nums[i][j] = startVal
		startVal++
	}
	for i, j := r2, c2-1; j >= c1; j-- {
		nums[i][j] = startVal
		startVal++
	}
	for i, j := r2-1, c1; i >= r1+1; i-- {
		nums[i][j] = startVal
		startVal++
	}
	_generateMatrix(r1+1, r2-1, c1+1, c2-1, startVal, nums)
}

func main() {
	n := 3
	matrix := generateMatrix(n)
	fmt.Println(matrix)
}
